package cn.qzzg.controller;


import cn.qzzg.common.ResultBean;
import cn.qzzg.dto.PagingQueryDTO;
import cn.qzzg.dto.TransferBoxDTO;
import cn.qzzg.pojo.SamplesPersonnel;
import cn.qzzg.pojo.TestTube;
import cn.qzzg.pojo.TransferBox;
import cn.qzzg.service.SamplesPersonnelService;
import cn.qzzg.service.TestTubeService;
import cn.qzzg.service.TransferBoxService;
import cn.qzzg.vo.TransferBoxVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/transferBox")
public class TransferBoxController {

    @Resource
    TransferBoxService transferBoxService;
    @Resource
    TestTubeService testTubeService;
    @Resource
    SamplesPersonnelService samplesPersonnelService;


    /**
     * 查询所有
     *
     * @return List<MyObjet>
     */
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public ResultBean<?> queryAll() {
        List<TransferBoxVO> results = new ArrayList<>();
        List<TransferBox> transferBoxList = transferBoxService.findAll();

        transferBoxList.forEach(transferBox -> {
            int count = testTubeService.findAllByTransferBoxId(transferBox.getTransferBoxId()).size();
            TransferBoxVO transferBoxVO = new TransferBoxVO();
            transferBoxVO.setTransferBoxId(transferBox.getTransferBoxId())
                    .setCreateTime(transferBox.getCreateTime())
                    .setInspectionTime(transferBox.getInspectionTime())
                    .setTestTubeNumber(count);
            results.add(transferBoxVO);
        });

        return ResultBean.success(results);
    }

    /**
     * 创建
     *
     * @return true
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResultBean<?> create(HttpServletRequest request, @RequestBody TransferBox transferBox) {
        transferBox.setReceivingPersonnel(request.getAttribute("name").toString());
        return ResultBean.success(transferBoxService.saveOne(transferBox));
    }

    /**
     * 删除
     *
     * @param myObjet 需要删除的
     * @return true
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultBean<?> delete(@RequestBody TransferBox myObjet) {
        List<TestTube> testTubeList = testTubeService.findAllByTransferBoxId(myObjet.getTransferBoxId());
        List<String> list2 = new ArrayList<>();
        testTubeList.forEach(testTube -> list2.add(testTube.getTestTubeId()));
        if (list2.size() != 0)
            testTubeService.batchRemove(list2);

        list2.forEach(id -> {
            List<SamplesPersonnel> samplesPersonnelList = samplesPersonnelService.findAllByTestTubeId(id);
            List<Integer> list = new ArrayList<>();
            samplesPersonnelList.forEach(samplesPersonnel -> list.add(samplesPersonnel.getSamplesPersonnelId()));
            if (list.size() != 0)
                samplesPersonnelService.batchRemove(list);
        });

        return ResultBean.success(transferBoxService.removeOne(myObjet.getTransferBoxId()));
    }

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    public ResultBean<?> queryPage(@RequestBody PagingQueryDTO<TransferBoxDTO> pagingQueryDTO) {
        Page<TransferBox> page = transferBoxService.findPage(pagingQueryDTO);
        List<TransferBox> transferBoxList = page.getRecords();
        List<TransferBoxVO> transferBoxVOList = new ArrayList<>();
        transferBoxList.forEach(transferBox -> {
            int count = testTubeService.findAllByTransferBoxId(transferBox.getTransferBoxId()).size();
            TransferBoxVO transferBoxVO = new TransferBoxVO();
            transferBoxVO.setTestTubeNumber(count)
                    .setTransferBoxId(transferBox.getTransferBoxId())
                    .setCreateTime(transferBox.getCreateTime())
                    .setSampleSourceId(transferBox.getSampleSourceId())
                    .setInspectionPersonnel(transferBox.getInspectionPersonnel())
                    .setInspectionPersonnelMobile(transferBox.getInspectionPersonnelMobile())
                    .setDictionaryInfoId(transferBox.getDictionaryInfoId());
            transferBoxVOList.add(transferBoxVO);
        });
        Page<TransferBoxVO> results = new Page<>();
        results.setCurrent(page.getCurrent())
                .setRecords(transferBoxVOList)
                .setSize(page.getSize());

        return ResultBean.success(results);
    }

    @RequestMapping(value = "/queryAllWeChat", method = RequestMethod.GET)
    public ResultBean<?> queryAllWeChat() {
        List<TransferBox> transferBoxList = transferBoxService.findAll();
        List<TransferBoxVO> transferBoxVOList = new ArrayList<>();
        transferBoxList.forEach(transferBox -> {
            int count = testTubeService.findAllByTransferBoxId(transferBox.getTransferBoxId()).size();
            TransferBoxVO transferBoxVO = new TransferBoxVO();
            transferBoxVO.setTestTubeNumber(count)
                    .setTransferBoxId(transferBox.getTransferBoxId())
                    .setCreateTime(transferBox.getCreateTime())
                    .setSampleSourceId(transferBox.getSampleSourceId())
                    .setInspectionPersonnel(transferBox.getInspectionPersonnel())
                    .setInspectionPersonnelMobile(transferBox.getInspectionPersonnelMobile())
                    .setDictionaryInfoId(transferBox.getDictionaryInfoId());
            transferBoxVOList.add(transferBoxVO);
        });

        return ResultBean.success(transferBoxVOList);
    }
}
